﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace WTSQCB
{
    //--Them vao cho nay-- Dai Ca Nam
    public class KetNoiDuLieu
    {
        public static int level = -1;
        public static string name = "";
        SqlConnection connect = new SqlConnection();
        public KetNoiDuLieu()
        {
            connect.ConnectionString = ConfigurationManager.ConnectionStrings["WebDiemTSQCBConnectionString"].ConnectionString;
            //Neu ket noi dong 
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();//thi mo ra
            }
        }
        
        public DataTable TaoBang(string sql)
        {
                //Khai báo đối tượng DataTable để chứa dữ liệu
                DataTable dt = new DataTable();
                //Khai báo đối tượng dùng để đọc dữ liệu dạng bảng
                SqlDataAdapter da = new SqlDataAdapter(sql, connect);
                da.Fill(dt);
                return (dt);
            }

        public DataTable LoadData(string sql)
            {
                SqlCommand cmd = new SqlCommand(sql, connect);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                return dt;
        }

        public DataTable LoadData(string sql, string[] name, object[] value, int Nparameter)
        {
                SqlCommand cmd = new SqlCommand(sql, connect);
                for (int i = 0; i < Nparameter; i++)
                {
                    cmd.Parameters.AddWithValue(name[i], value[i]);
                }
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                return dt;
        }
        public int UpdateData(string sql)
        {
                SqlCommand cmd = new SqlCommand(sql, connect);
                //SqlDataReader rdr = cmd.ExecuteReader();
                cmd.CommandType = CommandType.StoredProcedure;
                int kq = cmd.ExecuteNonQuery();
                return kq;
        }

        public int UpdateData(string sql, string[] name, object[] value, int Nparameter)
        {
                SqlCommand cmd = new SqlCommand(sql, connect);
                //SqlDataReader rdr = cmd.ExecuteReader();
                for (int i = 0; i < Nparameter; i++)
                {
                    cmd.Parameters.AddWithValue(name[i], value[i]);
                }
                cmd.CommandType = CommandType.StoredProcedure;
                int kq = cmd.ExecuteNonQuery();
                return kq;
         }
    }

    
}